//
//  RTJLoginOrRegisterController.m
//  RuiTaiJinProject
//
//  Created by jayson on 2018/12/1.
//  Copyright © 2018年 leewei. All rights reserved.
//

#import "RTJLoginOrRegisterController.h"
#import "RTJLoginController.h"
#import "RTJRegisterController.h"

@interface RTJLoginOrRegisterController ()<UITextFieldDelegate>

@property (nonatomic,assign)BOOL isLogin;
@property (weak, nonatomic) IBOutlet UITextField *phoneTxt;
@property (weak, nonatomic) IBOutlet UIButton *selectedBtn;

@property (weak, nonatomic) IBOutlet UIButton *nextBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *viewTop;
@end

@implementation RTJLoginOrRegisterController

-(void)viewWillAppear:(BOOL)animated{
    
    if ([RTJUserTool isLogin]) {
        
        [self.navigationController dismissViewControllerAnimated:NO completion:nil];
    }
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.viewTop.constant = screenH == 812 ? 110 : 90; self.navigationController.navigationBarHidden = NO;
    
    self.title = @"手机快速注册/登陆";
    
    _selectedBtn.selected = YES;
    _phoneTxt.delegate = self;
    self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"nav_cha"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    
    _nextBtn.userInteractionEnabled = NO;
    [_nextBtn setBackgroundImage:[UIImage imageNamed:@"btn_qrcjhui"] forState: UIControlStateNormal];
    
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    
    _nextBtn.userInteractionEnabled = _phoneTxt.text.length!=0 ? YES : NO;
    [_nextBtn setBackgroundImage:_phoneTxt.text.length!=0 ?[UIImage imageNamed:@"btn_ljcj"]:[UIImage imageNamed:@"btn_qrcjhui"] forState: UIControlStateNormal];
    
}

- (IBAction)next:(UIButton *)sender {
    
    [SVProgressHUD showWithStatus:nil];
    
    if (![Apputil validatePhoneWithString:_phoneTxt.text]) {
        
        [SVProgressHUD showErrorWithStatus:@"请正确填写您的电话号码!"];
        return;
    }
    
    if (_selectedBtn.selected != YES) {
        
        
        [SVProgressHUD showErrorWithStatus:@"请仔细阅读《服务协议》"];
        return;
    }
    
    NSDictionary *dic = @{@"mobile":_phoneTxt.text};
    
    [RTJNetWork  asyncRequestWithURL:@"user/check_reg_mobile" params:dic method:POST zxCompletion:^(id data, NSInteger status, BOOL success, NSString *errorMsg) {
        
        [SVProgressHUD dismiss];
        
        if (success) {
            
            if ([data[@"status"]integerValue] == 1) {
                
                RTJRegisterController *VC = [[RTJRegisterController alloc]init];
                VC.title = @"注册";
                VC.phoneStr = self.phoneTxt.text;
                [self.navigationController pushViewController:VC animated:YES];
                
            }else{
                
                RTJLoginController *VC = [[RTJLoginController alloc]init];
                VC.phoneStr = self.phoneTxt.text;
                [self.navigationController pushViewController:VC animated:YES];
                
            }
            
        }else{
            
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",errorMsg]];

        }
       
    }];
    
}

- (IBAction)selected:(UIButton *)sender {

    sender.selected = !sender.selected;

}


- (IBAction)service:(id)sender {
    
    RTJHTMLViewController *vc = [[RTJHTMLViewController alloc]init];
    vc.title = @"服务协议";
    vc.htmlStr = @"zhucexieyi";
    [self.navigationController pushViewController:vc animated:YES];

    
}


-(void)back{
    
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    
}



@end
